package com.marvels.approve.mqueues.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.marvels.approve.loan.service.IMcamLoanApplyInfoService;
import com.marvels.approve.mqueues.dao.ILoanApplyQueueDao;
import com.marvels.approve.mqueues.dto.LoanApplyQueueDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyQueueService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
@Service
public class ApproveLoanApplyQueueServiceImpl implements ApproveILoanApplyQueueService {
	@Autowired
	private ILoanApplyQueueDao loanApplyQueueDao; 
	@Autowired
	private IMcamLoanApplyInfoService mcamLoanApplyInfoService;
	@Override
	public LoanApplyQueueDTO saveLoanApplyQueueService(LoanApplyQueueDTO loanApplyQueue)
			throws MarvelsBusinessException {
		this.loanApplyQueueDao.insertLoanApplyQueue(loanApplyQueue);
		return loanApplyQueue;
	}

	@Override
	public LoanApplyQueueDTO editLoanApplyQueueService(LoanApplyQueueDTO loanApplyQueue)
			throws MarvelsBusinessException {
		// 进件详情queue同步跟踪变更
		Long id = loanApplyQueue.getId();
		LoanApplyQueueDTO dto = this.loanApplyQueueDao.selectLoanApplyQueueById(id);
		
		if(dto==null){
			throw new MarvelsBusinessException(id+"不存在");
		}
		
		this.loanApplyQueueDao.updateLoanApplyQueue(loanApplyQueue);
		return loanApplyQueue;
	}

	@Override
	public List<LoanApplyQueueDTO> findLoanApplyQueueService(LoanApplyQueueDTO loanApplyQueue)
			throws MarvelsBusinessException {
		// 一次处理20条数据
		if(loanApplyQueue.getCurPage()!=null) {
			PageHelper.startPage(loanApplyQueue.getCurPage()==null?0:loanApplyQueue.getCurPage(),loanApplyQueue.getPageSize()==null?20:loanApplyQueue.getPageSize());
		}
		
		return this.loanApplyQueueDao.selectLoanApplyQueue(loanApplyQueue);
	}

	@Override
	public LoanApplyQueueDTO findLoanApplyQueueService(Long id) throws MarvelsBusinessException {
		LoanApplyQueueDTO loanApplyQueue = this.loanApplyQueueDao.selectLoanApplyQueueById(id);
		return loanApplyQueue;
	}

	@Override
	public Integer findLoanApplyQueueCount() throws MarvelsBusinessException {
		
		return this.loanApplyQueueDao.selectLoanApplyQueueCount();
	}

	
	/**保存
	 * @author 何杰
	 * 2018年5月10日
	 */
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	public LoanApplyQueueDTO processQueue(String loanId)throws MarvelsBusinessException{
		// 0进件详情queue同步跟踪
		LoanApplyQueueDTO loanApplyQueue = new LoanApplyQueueDTO();
		loanApplyQueue.setLoanId(loanId);
		loanApplyQueue.setStatus(0);
		loanApplyQueue.setDeleteFlag(0);
		loanApplyQueue.setCreatedBy("SYSTEM");
		loanApplyQueue.setUpdatedBy("SYSTEM");
		loanApplyQueue = saveLoanApplyQueueService(loanApplyQueue);
		MarvelsLogUtil.getInstance().info("queues loan_Apply_Queue:" + loanApplyQueue);
		return loanApplyQueue;
	}
	/**处理逻辑
	 * @author 何杰
	 * 2018年5月9日
	 * @param loanId
	 * @throws Exception
	 */
	@Transactional(timeout = 60 ,rollbackFor=Exception.class)
	public void processItem(LoanApplyQueueDTO loanApplyQueue)throws MarvelsBusinessException{
		mcamLoanApplyInfoService.initProcessData(loanApplyQueue);
	}

	@Override
	public List<LoanApplyQueueDTO> selectExpiredLoanApplyQueue(LoanApplyQueueDTO loanApplyQueue) {
		// 一次处理20条数据
		if(loanApplyQueue.getCurPage()!=null) {
			PageHelper.startPage(loanApplyQueue.getCurPage()==null?0:loanApplyQueue.getCurPage(),loanApplyQueue.getPageSize()==null?20:loanApplyQueue.getPageSize());
		}
		
		return this.loanApplyQueueDao.selectExpiredLoanApplyQueue(loanApplyQueue);
	}
	
	
	public LoanApplyQueueDTO findLoanApplyQueueLoanId(String loanId) {
		return this.loanApplyQueueDao.findLoanApplyQueueLoanId(loanId);
	}
}
